package com.javaspeak.concurrency.synchronizedperformance.contended;

import java.util.concurrent.atomic.AtomicInteger;

import com.lmax.disruptor.Sequence;

/**
 * This class has 3 ways of incrementing a counter. One is by using a
 * synchronized method, the other is using an AtomicInteger and the final way
 * is using the Disruptor's Sequence.
 * 
 * @author John Dickerson
 */
public class ContendedCounter {

    private int countSynchronized = -1;
    private AtomicInteger count = new AtomicInteger( -1 );
    private Sequence sequence = new Sequence( -1 );
   
    
    public synchronized int incrementCountSynchronizedAndContended(){
      
        return ++countSynchronized;
    }
    
    
    public int incrementCountCasContended(){
        
        return count.incrementAndGet();
    }
    
    
    public int incrementSequenceFromDisruptor(){
     
        return ( int )sequence.incrementAndGet();
    }
}
